#include "WordBuffer.h"


WordBuffer::WordBuffer(void)
{
	head.c = -1;
	head.col = -1;
	head.row = -1;
	head.next = NULL;

	last = &head;

	count = 0;
}


WordBuffer::~WordBuffer(void)
{
	if (count > 0)
	{
		getWord(count);
	}
}
void WordBuffer::pushChar(char c, int  row, int  col)
{
	Char* newChar = new Char;

	newChar->c = c ;
	newChar->row = row;
	newChar->col = col;
	newChar->next = NULL;

	last->next = newChar;

	last = newChar;

	count++;
}
char WordBuffer::readCurrentTop()
{
	return last->c;
}
bool WordBuffer::getWord(int  length, char  *dest)
{
	if(length > count||length <= 0)
		return  false;
	
	if(length == count)
		last = &head;
	
	char  * buffer = new char[length+1];
	Char  * tmp = head.next;
	for ( int  i = 0 ; i < length ; i ++)
	{
		buffer[i] = tmp->c;
		buffer[i+1] = 0;
		Char  * tmp1 = tmp;
		tmp = tmp->next;
		delete tmp1;
		count--;
	}

	head.next = tmp;

	if(dest!=NULL)
	{
		memcpy(dest,buffer,length+1);
	}

	delete [] buffer;
}
int  WordBuffer::getBufferLength()
{
	return count;
}
Char  WordBuffer::operator[](int  index)
{
	Char  result;

	result.c = -1;
	result.col = -1;
	result.row = -1;
	result.next = NULL;

	if(index>=count)
		return result;

	Char  *  tmp = head.next;
	
	for(int i =0 ; i < index; i++)
	{
		tmp = tmp->next;
	}

	result.c = tmp->c;
	result.col = tmp->col;
	result.row = tmp->row;
	result.next = NULL;

	return  result;
}